const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
  showMsg
} from '../../../../utils/config.js'
import {
  reqConsumerRoomAdd,
} from '../../../../request/api.js';
Page({
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离

    familyInfo: {},

    roomName: "",
    list: ['客厅', '主卧', '次卧', '餐厅', '厨房', '书房', '阳台', '儿童房', '衣帽间'],

    confirmButtonDisabled: false,
  },

  onLoad(options) {
    this.getParams()
  },
  onShow() {

  },

  getParams() {
    const _this = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('sendDataToAdd', function (res) {
      console.log(res);
      _this.setData({
        familyInfo: res.data
      })
      console.log('form', _this.data.familyInfo);
    })
  },


  getName(item) {
    this.setData({
      roomName: item.currentTarget.dataset.item
    })
  },

  async confirm() {
    if (this.data.confirmButtonDisabled) return
    console.log(this.data.familyInfo);
    const {
      roomName,
      familyInfo
    } = this.data
    const res = await reqConsumerRoomAdd({
      roomName,
      homeId: familyInfo.id,
    })
    if (res.code != 20000) return showMsg(res.message);
    showMsg("添加成功", 'success')
    app.globalData.isUpdateCIndex = true
    wx.setStorageSync('isUpdateCIndex', true);
    this.setData({
      confirmButtonDisabled: true
    })

    setTimeout(_ => {
      wx.navigateBack()
      this.notifyPageUpdataFoo()
    }, 1000)
  },

  notifyPageUpdataFoo() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('receiveDataFromAdd', {
      isUpdataList: true
    });
  },

})